<?php

/*
 * This page is for validate pre-purchased code amount and store in profile
 */
include '../include/config.php';

session_start();
$user_name = $_SESSION['user_name'];
$sur_name = $_SESSION['sur_name'];
$password = $_SESSION['password'];
$email = $_SESSION['email'];
$key = '360technosoft';
function _encode($password, $key) {
    $majorsalt = null;
    if ($key != '') {
        $_password = $key . $password;
    } else {
        $_password = $password;
    }
    if (function_exists('str_split')) {
        $_pass = str_split($_password);
    } else {
        $_pass = array();
        if (is_string($_password)) {
            for ($i = 0; $i < strlen($_password); $i++) {
                array_push($_pass, $_password[$i]);
            }
        }
    }

    foreach ($_pass as $_hashpass) {
        $majorsalt .= md5($_hashpass);
    }
    return $password = md5($majorsalt);
}

$babyproductid = 2;
$discount = $_POST['discount'];
$buy = mysql_query("select * from salus_buy_detail where FK_fa_product_id = '" . $babyproductid . "' ") or die(mysql_error());
$dis1 = $discount/100;
$pad = $_POST['paid'];
$i=1;
while ($row = mysql_fetch_array($buy)) {
    $maincost = $row[3];
    $dis2= $maincost*$dis1;
    $paid = $maincost-$dis2;    
    if($paid==$pad)
    {                
        break;
    }
    else{
        $i++;
    }
}
$year= $i;
echo 'Configuring your account please wait<br>';
$password = _encode($password, $key);
/*
 * inserting data to main user table.
 */
mysql_query("insert into fa_user(`id`,`user_name`,`password`,`email`,`role`,`sur_name`) values(NULL,'".$user_name."', '".$password."','".$email."','user','".$sur_name."' )") or die(mysql_error()) ;
echo 'Done<br>';
$uid = mysql_insert_id();
echo $uid;
/*
 * inserting data into buy_products.
 */
mysql_query("insert into salus_buy_product(`id`,`FK_fa_user_id`,`FK_fa_product_id`,`product_prize`,`date`) values(NULL,'".$uid."',2,'".$pad."','".  date('Y-m-d')."') ") or die(mysql_error());
echo 'Done<br>';
/*
 * inserting into prepaid code table
 */
mysql_query("update salus_prepaid_codes set FK_fa_user_id='".$uid."' where prepaid_code='".$_SESSION['prepaid_code']."' ") or die(mysql_error());
echo 'Done<br>';
/*
 * inserting into child profile database
 */
if($_SESSION['gender']=='M')
{
    $idd= 'father_id';
}
else{
    $idd= 'mother_id';
}
mysql_query("insert into baby_child_profile_detail (bid,buyer_id,".$idd.") values(NULL,'".$uid."','".$uid."') ") or die(mysql_error());
echo 'Done<br>';
/*
 * inserting into user_profile
 */
mysql_query("insert into salus_user_profile_detail (id,FK_fa_user_id,name,sur_name,sex,email) value(NULL,'".$uid."','".$user_name."','".$sur_name."','".$_SESSION['gender']."','".$email."') ") or die(mysql_error());
echo 'Done<br>';

echo 'Account created successfully. goto <a href="http://'.$_SERVER['HTTP_HOST'].'/salusbaby"> home page</a> for login to your profile.';


?>
